package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.platform.dto.BasicDeviceFaultRecordDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.mapper.BasicDeviceFaultRecordMapper;
import com.iwomy.secureplat.platform.service.BasicDeviceFaultRecordService;
import com.iwomy.secureplat.platform.vo.BasicDeviceFaultRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 故障记录表
 *
 * @author pig
 * @date 2023-11-23 10:49:50
 */
@Service
public class BasicDeviceFaultRecordServiceImpl extends ServiceImpl<BasicDeviceFaultRecordMapper, BasicDeviceFaultRecordEntity> implements BasicDeviceFaultRecordService {

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Override
	public Page<BasicDeviceFaultRecordVO> getBasicDeviceFaultRecordPage(Page page, BasicDeviceFaultRecordDTO basicDeviceFaultRecordDTO) {
		//多表查询
		MPJLambdaWrapper<BasicDeviceFaultRecordEntity> wrapper = wrapper(basicDeviceFaultRecordDTO);
		//子查询部分
		String subQuery = "(SELECT MAX(create_time) FROM basic_device_fault_record  WHERE imei = t.imei)";
		wrapper.apply("t.create_time = " + subQuery);

		return this.getBaseMapper().selectJoinPage(page, BasicDeviceFaultRecordVO.class, wrapper);

	}

	@Override
	public Page<BasicDeviceFaultRecordVO> getImeiList(Page page, BasicDeviceFaultRecordDTO basicDeviceFaultRecordDTO) {
		MPJLambdaWrapper<BasicDeviceFaultRecordEntity> wrapper = wrapper(basicDeviceFaultRecordDTO);
		return this.getBaseMapper().selectJoinPage(page, BasicDeviceFaultRecordVO.class, wrapper);
	}

	@Override
	public BasicDeviceFaultRecordVO getById(Long id) {
		// 多表查询
		MPJLambdaWrapper<BasicDeviceFaultRecordEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.selectAll(BasicDeviceFaultRecordEntity.class)
				.selectAs(BasicInstallLocationEntity::getAddress, BasicDeviceFaultRecordVO::getInstallAddress)
				.selectAs(BasicDeviceTypeEntity::getTypeName, BasicDeviceFaultRecordVO::getDeviceType)
				.selectAs(BasicDeviceModelEntity::getDeviceModelName, BasicDeviceFaultRecordVO::getDeviceModelName)
				.leftJoin(BasicInstallLocationEntity.class, BasicInstallLocationEntity::getId, BasicDeviceFaultRecordEntity::getInstallLocationId)
				.leftJoin(BasicDeviceTypeEntity.class, BasicDeviceTypeEntity::getId, BasicDeviceFaultRecordEntity::getDeviceTypeId)
				.leftJoin(BasicDeviceModelEntity.class, BasicDeviceModelEntity::getId, BasicDeviceFaultRecordEntity::getDeviceModelId)
				.eq(BasicDeviceFaultRecordEntity::getId, id);

		BasicDeviceFaultRecordVO basicDeviceFaultRecord = this.getBaseMapper().selectJoinOne(BasicDeviceFaultRecordVO.class, wrapper);

		if (basicDeviceFaultRecord != null) {
			Long deptId = basicDeviceFaultRecord.getDeptId();
			if (deptId != null) {
				String deptName = remoteDeptUtils.getDeptName(deptId);
				basicDeviceFaultRecord.setDeptName(deptName);
			}
		}

		return basicDeviceFaultRecord;
	}


	public static MPJLambdaWrapper<BasicDeviceFaultRecordEntity> wrapper(BasicDeviceFaultRecordDTO basicDeviceFaultRecordDTO) {
		// 多表查询
		MPJLambdaWrapper<BasicDeviceFaultRecordEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.selectAll(BasicDeviceFaultRecordEntity.class)
				.selectAs(BasicDeviceTypeEntity::getTypeName, BasicDeviceFaultRecordVO::getDeviceType)
				.selectAs(BasicDeviceModelEntity::getDeviceModelName, BasicDeviceFaultRecordVO::getDeviceModelName)
				.leftJoin(BasicDeviceTypeEntity.class, BasicDeviceTypeEntity::getId, BasicDeviceFaultRecordEntity::getDeviceTypeId)
				.leftJoin(BasicDeviceModelEntity.class, BasicDeviceModelEntity::getId, BasicDeviceFaultRecordEntity::getDeviceModelId);

		if (ObjectUtil.isNotEmpty(basicDeviceFaultRecordDTO.getDeptId())) {
			wrapper.eq(BasicDeviceFaultRecordEntity::getDeptId, basicDeviceFaultRecordDTO.getDeptId());
		}
		if (ObjectUtil.isNotEmpty(basicDeviceFaultRecordDTO.getDeviceTypeId())) {
			wrapper.eq(BasicDeviceFaultRecordEntity::getDeviceTypeId, basicDeviceFaultRecordDTO.getDeviceTypeId());
		}
		if (ObjectUtil.isNotEmpty(basicDeviceFaultRecordDTO.getDeviceModelId())) {
			wrapper.eq(BasicDeviceFaultRecordEntity::getDeviceModelId, basicDeviceFaultRecordDTO.getDeviceModelId());
		}
		if (ObjectUtil.isNotEmpty(basicDeviceFaultRecordDTO.getDescription())) {
			wrapper.like(BasicDeviceFaultRecordEntity::getDescription, basicDeviceFaultRecordDTO.getDescription());
		}
		if (ObjectUtil.isNotEmpty(basicDeviceFaultRecordDTO.getState())) {
			wrapper.like(BasicDeviceFaultRecordEntity::getState, basicDeviceFaultRecordDTO.getState());
		}
		if (ObjectUtil.isNotEmpty(basicDeviceFaultRecordDTO.getImei())) {
			wrapper.eq(BasicDeviceFaultRecordEntity::getImei, basicDeviceFaultRecordDTO.getImei())
					.orderByDesc(BasicDeviceFaultRecordEntity::getCreateTime);
		}
		return wrapper;
	}
}